﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;

public partial class ShoppingCard : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie hc = null;
        if (Request.Cookies["ShoppingCart"] == null)
        {
            //如果Cookies中不存在ShoppingCart，则创建
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            Label lab = new Label();
            lab.Text = "购物车为空";
            SetOderBut.Enabled = false;
            cell = new TableCell();
            cell.Width = new Unit(100);
            cell.Controls.Add(lab);
            row.Cells.Add(cell);
            CardTable.Rows.Add(row);
        }
        else
        {
            for (int i = 0; i < Request.Cookies["ShoppingCart"].Values.Count; ++i)
            {
                string subKeyName, subKeyValue;
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                Label lab = new Label();

                subKeyName = Server.HtmlEncode(Request.Cookies["ShoppingCart"].Values.AllKeys[i]);
                subKeyValue = Server.HtmlEncode(Request.Cookies["ShoppingCart"].Values[i]);
                if (subKeyName == null)
                {
                    lab.Text = "购物车为空";
                    SetOderBut.Enabled = false;
                    cell = new TableCell();
                    cell.Width = new Unit(100);
                    cell.Controls.Add(lab);
                    row.Cells.Add(cell);
                    CardTable.Rows.Add(row);
                    break;
                }
                row = new TableRow();
                cell = new TableCell();

                lab = new Label();
                lab.Text = subKeyName;
                cell = new TableCell();
                cell.Width = new Unit(100) ;
                cell.Controls.Add(lab);
                row.Cells.Add(cell);

                Label lab2 = new Label();
                lab2.Text = getDishName(subKeyName);
                cell = new TableCell();
                cell.Width = new Unit(100);
                cell.Controls.Add(lab2);
                row.Cells.Add(cell);

                Label lab3 = new Label();
                lab3.Text = subKeyValue;
                cell = new TableCell();
                cell.Width = new Unit(100);
                cell.Controls.Add(lab3);
                row.Cells.Add(cell);

                Button addBut = new Button();
                addBut.CommandArgument = subKeyName;
                addBut.Command += new CommandEventHandler(this.addBut_Click);
                addBut.Text = "增加";
                cell = new TableCell();
                cell.Controls.Add(addBut);
                row.Cells.Add(cell);

                Button delBut = new Button();
                delBut.Text = "减少";
                delBut.CommandArgument = subKeyName;
                delBut.Command += new CommandEventHandler(this.delBut_Click);
                cell = new TableCell();
                cell.Controls.Add(delBut);
                row.Cells.Add(cell);

                CardTable.Rows.Add(row);
            }
        }        
    }
    protected void addBut_Click(object sender, CommandEventArgs  e)
    {
        
        string no = e.CommandArgument.ToString();
        //读取选中内容
 
        HttpCookie sc = createShoppingCart();
        string key = no;
        if (sc[key] != null)
        {//在
            sc[key] = (int.Parse(sc[key]) + 1).ToString();
        }
        else
        {//不在
            sc[key] = "1";
        }

        Response.Cookies.Add(sc);
        Response.AddHeader("Refresh", "0");  //刷新当前页面
    }

    protected void delBut_Click(object sender, CommandEventArgs e)
    {
        string no = e.CommandArgument.ToString();
        //读取选中内容

        HttpCookie sc = createShoppingCart();
        string key = no;
        if (int.Parse(sc[key]) == 0)
        {
            return;
        }
        if (sc[key] != null)
        {//在
            sc[key] = (int.Parse(sc[key]) - 1).ToString();
        }

        Response.Cookies.Add(sc);
        Response.AddHeader("Refresh", "0");  //刷新当前页面
    }

    protected void ClearCardBut_Click(object sender, EventArgs e)
    {
        HttpCookie hc = new System.Web.HttpCookie("ShoppingCart");
        Response.Cookies.Add(hc);
        Response.AddHeader("Refresh", "0");  //刷新当前页面
        return;
    }

    private string getDishName(string no)
    {
        string name  = null;

        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrCustomer"].ConnectionString;


        string sqlString = "select Dname from Customer_Dish where Dno = @no";
        SqlParameter[] parameters = {
                                        new SqlParameter("@no",no)
                                    };
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    DataSet ds = new DataSet();
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(ds);
                    DataTable table = ds.Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        name = table.Rows[0][0].ToString();
                    }                    
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }

        }
        return name;
    }

    private HttpCookie createShoppingCart()
    {
        HttpCookie hc = null;
        if (Request.Cookies["ShoppingCart"] == null)
        {
            //如果Cookies中不存在ShoppingCart，则创建
            Dictionary<string, int> cart = new Dictionary<string, int>();
            hc = new HttpCookie("ShoppingCart");
        }
        else
        {
            //如果Cookies中存在ShoppingCart，则取出
            hc = Request.Cookies["ShoppingCart"];
        }
        return hc;
    }

    protected void SetOderBut_Click(object sender, EventArgs e)
    {
        Response.Redirect("./SetOrder.aspx");
    }
}